<?php
class PhotoController extends Zend_Controller_Action
{

    private $user_id;

    private $user_login;

    function init () {
        $this->user_id = Zend_Auth::getInstance()->getIdentity()->id;
        $this->user_login = Zend_Auth::getInstance()->getIdentity()->login;
        
        $init = new Model_Init($this);
    }

    function indexAction () {
        $form = new Form_Album();
        
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                
                $photo = new Model_Photo($this->user_login);
                $photo->fill($form->getValues());
                $photo->created = date("Y-m-d H:i:s", time());
                $photo->user_id = $this->user_id;
                
                $photo->save();
                
                $photo->createAlbum($photo->getLastInsertedId());
                echo $this->view->translate('Album was created');
            }
        }
        $this->view->form = $form;
        $this->view->dir = $this->view->baseUrl(
        'album' );
        
        
        $user = $this->_getParam('request');
        $photo = new Model_Photo();
        
        if ($user != '') {
            $user_model = new Model_User();
            $user_id = $user_model->getUserInfoByLogin($user);
            $this->view->photo_albums = $photo->getUserAlbumList($user_id->id);
            $this->view->user = $user;
        } else {
            $this->view->photo_albums = $photo->getUserAlbumList($this->user_id);
            $this->view->user = $this->user_login;
        }
        
    }

    function albumAction () {
        
        //catch params from url
        $param = $this->_getParam('request');

        //if there are some requests params
        if (is_array($param)) {
            $user =  $param[0];
            $album_id = $param[1];
        } else {
            $user = $this->user_login;
            $album_id = $param;
        }
        
        //upload photo
        $form = new Form_Photo();
        
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                
                $photo = new Model_Photo($this->user_login);
                
                $file = $form->photo->getFileInfo();
                
                $file_name = pathinfo($form->photo->getFileName());
                $newFilename = 'file-' . uniqid() . '.' . $file_name['extension'];
                
                $ready_file_path = realpath(dirname('.')) . DIRECTORY_SEPARATOR .
                 'album' . DIRECTORY_SEPARATOR . $this->user_login .
                 DIRECTORY_SEPARATOR . $album_id . DIRECTORY_SEPARATOR;
                
                $form->photo->addFilter('Rename', 
                $ready_file_path . $newFilename);
                $form->photo->receive();
                
                //resize full size image
                $thumb = Gd_PhpThumbFactory::create(
                $ready_file_path . $newFilename);
                $thumb->resize(800, 533)->save($ready_file_path . $newFilename);
                
                //create thumbnail for image
                $thumb_url = $ready_file_path . 'thumb' . DIRECTORY_SEPARATOR . $newFilename;
                $thumb->resize(150, 100)->save($thumb_url);
                
                echo $this->view->translate('Photo was uploaded');
                
                 //add action that user add new photo to album
                $model_action = new Model_Action();
                $model_action->addAction($this->user_id, 3, $album_id, $newFilename);
            }
        }
        $this->view->form = $form;
        
        $photo = new Model_Photo($user);
        
        $this->view->album_id = $album_id;
        $this->view->user = $user;
        $this->view->photos = $photo->getPhotosList($album_id);
        $this->view->dir = $this->view->baseUrl(
        'album' . DIRECTORY_SEPARATOR . $user . DIRECTORY_SEPARATOR .
         $album_id);
    
    }
    
    function removealbumAction () {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $album_id = $this->_getParam('request');
        
        $model_photo = new Model_Photo($this->user_login,$album_id);
        $model_photo->removeAlbum();
        
        $this->_helper->redirector('index', 'photo');
    }
    
    public function removephotoAction () {
            if (Zend_Auth::getInstance()->hasIdentity()) {
                $this->_helper->layout->disableLayout();
                $this->_helper->viewRenderer->setNoRender(true);

                if ($this->getRequest()->isPost()) {
                    
                    $album_id = $this->_getParam('album_id');
                    $photo_name = $this->_getParam('photo_id');
                    
                    $user_name = $this->user_login;
                    $model_photo = new Model_Photo($user_name);
                    $model_photo->removePhoto($album_id, $photo_name);
                    //get ID user from session
//                    $id = Zend_Auth::getInstance()->getIdentity()->id;
//                    $user = new Model_User($id);
//                    $user->status_x = $this->_getParam('x_coordinate');
//                    $user->status_y = $this->_getParam('y_coordinate');
//                    $user->save();
                }
            }
    }

}